package com.uchiha.nova.core.tool.support;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.uchiha.nova.core.tool.util.AuthUtil;
import com.uchiha.nova.core.tool.util.BeanUtil;
import com.uchiha.nova.core.tool.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * @author 周林静
 * @Date 2022-01-13 11:05
 * @Version 1.0
 */
@Validated
@Slf4j
public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> extends ServiceImpl<M, T> implements BaseService<T> {
    public BaseServiceImpl() {
    }

    public boolean save(T entity) {
        this.resolveEntity(entity);
        return super.save(entity);
    }

    public boolean saveBatch(Collection<T> entityList, int batchSize) {
        entityList.forEach(this::resolveEntity);
        return super.saveBatch(entityList, batchSize);
    }

    public boolean updateById(T entity) {
        this.resolveEntity(entity);
        return super.updateById(entity);
    }

    public boolean updateBatchById(Collection<T> entityList, int batchSize) {
        entityList.forEach(this::resolveEntity);
        return super.updateBatchById(entityList, batchSize);
    }

    public boolean saveOrUpdate(T entity) {
        return entity.getId() == null ? this.save(entity) : this.updateById(entity);
    }

    public boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize) {
        entityList.forEach(this::resolveEntity);
        return super.saveOrUpdateBatch(entityList, batchSize);
    }

    @Transactional(
            rollbackFor = {Exception.class}
    )
    public boolean deleteLogic(List<Long> ids) {
        NovaUser user = AuthUtil.getUser();
        List<T> list = new ArrayList();
        ids.forEach((id) -> {
            T entity = BeanUtil.newInstance(this.getEntityClass());
            if (user != null) {
                entity.setUpdateUser(user.getUserId());
            }

            entity.setUpdateTime(DateUtil.fromDate(new Date()));
            entity.setId(id);
            list.add(entity);
        });
        return super.updateBatchById(list) && super.removeByIds(ids);
    }

    @Override
    public boolean deleteOneLogic(Long id) {
        NovaUser user = AuthUtil.getUser();
        T entity = BeanUtil.newInstance(this.getEntityClass());
        entity.setUpdateTime(DateUtil.fromDate(new Date()));
        entity.setUpdateUser(user.getUserId());
        entity.setId(id);
        return super.updateById(entity)&&super.removeById(id);
    }


    private void resolveEntity(T entity) {
        try {
            NovaUser user = AuthUtil.getUser();
            LocalDateTime now = DateUtil.fromDate(new Date());
            if(ObjectUtil.isNotEmpty(user)){

                Long userId = user.getUserId();
                if(ObjectUtil.isNotEmpty(entity.getId())){
                    entity.setUpdateTime(now);
                    entity.setUpdateUser(userId);
                }else{
                    if(ObjectUtil.isNotEmpty(user.getTenantId())&&ObjectUtil.isEmpty(entity.getTenantId())){
                        entity.setTenantId(user.getTenantId());
                    }

                    if(ObjectUtil.isNotEmpty(user.getCompanyId())&&ObjectUtil.isEmpty(entity.getCompanyId())){
                        entity.setCompanyId(Long.valueOf(user.getCompanyId()));
                    }
                    if(ObjectUtil.isNotEmpty(user.getDeptId())&&ObjectUtil.isEmpty(entity.getDeptId())){
                        entity.setDeptId(Long.valueOf(user.getDeptId()));
                    }
                    if(ObjectUtil.isNotEmpty(user.getPostId())&&ObjectUtil.isEmpty(entity.getPostId())){
                        entity.setPostId(Long.valueOf(user.getPostId()));
                    }
                    if(ObjectUtil.isEmpty(entity.getStatus())){
                        entity.setStatus(0);
                    }
                    entity.setCreateUser(userId);
                    entity.setCreateTime(now);
                    entity.setUpdateUser(userId);
                    entity.setUpdateTime(now);
                    entity.setDeleted(0);
                }
            }else{
                if(ObjectUtil.isNotEmpty(entity.getId())){
                    entity.setUpdateTime(now);
                }else{
                    if(ObjectUtil.isEmpty(entity.getStatus())){
                        entity.setStatus(0);
                    }
                    entity.setCreateTime(now);
                    entity.setUpdateTime(now);
                    entity.setDeleted(0);
                }


            }



        } catch (Throwable var8) {
            throw var8;
        }
    }
    public void initEntity(T entity,NovaUser user) {
        try {
            LocalDateTime now = DateUtil.fromDate(new Date());
            if(ObjectUtil.isEmpty(user)){

                Long userId = user.getUserId();
                if(ObjectUtil.isNotEmpty(entity.getId())){
                    entity.setUpdateTime(now);
                    entity.setUpdateUser(userId);
                }else{
                    if(ObjectUtil.isNotEmpty(user.getTenantId())&&ObjectUtil.isEmpty(entity.getTenantId())){
                        entity.setTenantId(user.getTenantId());
                    }

                    if(ObjectUtil.isNotEmpty(user.getCompanyId())&&ObjectUtil.isEmpty(entity.getCompanyId())){
                        entity.setCompanyId(Long.valueOf(user.getCompanyId()));
                    }
                    if(ObjectUtil.isNotEmpty(user.getDeptId())&&ObjectUtil.isEmpty(entity.getDeptId())){
                        entity.setDeptId(Long.valueOf(user.getDeptId()));
                    }
                    if(ObjectUtil.isNotEmpty(user.getPostId())&&ObjectUtil.isEmpty(entity.getPostId())){
                        entity.setPostId(Long.valueOf(user.getPostId()));
                    }
                    if(ObjectUtil.isEmpty(entity.getStatus())){
                        entity.setStatus(0);
                    }
                    entity.setCreateUser(userId);
                    entity.setCreateTime(now);
                    entity.setUpdateUser(userId);
                    entity.setUpdateTime(now);
                    entity.setDeleted(0);
                }
            }else{
                if(ObjectUtil.isNotEmpty(entity.getId())){
                    entity.setUpdateTime(now);
                }else{
                    if(ObjectUtil.isEmpty(entity.getStatus())){
                        entity.setStatus(0);
                    }
                    entity.setCreateTime(now);
                    entity.setUpdateTime(now);
                    entity.setDeleted(0);
                }


            }



        } catch (Throwable var8) {
            throw var8;
        }
    }
}
